package cn.chinaunicom.mini.program.dingtalk.events.stream;

import cn.chinaunicom.mini.program.dingtalk.events.publisher.DingTalkPublisher;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import lombok.Builder;

/**
 * @ProjectName: dingtalk-adapter
 * @Package: cn.chinaunicom.mini.program.dingtalk.stream
 * @ClassName: DingTalkEventStreamBuilder
 * @Description: 钉钉事件订阅
 * @Author: wangjc
 * @CreateDate: 2024/6/21
 * @UpdateUser: wangjc
 * @UpdateDate: 2024/6/21
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */

@Builder
public class DingTalkEventStreamConfig {

    /**
     *  钉钉应用id
     */
    private String clientId;

    /**
     *  钉钉应用密钥
     */
    private String clientSecret;

    /**
     *  事件发布器
     */
    private DingTalkPublisher publisher;

    public void start() throws Exception {
        OpenDingTalkStreamClientBuilder
                .custom()
                .credential(new AuthClientCredential(clientId, clientSecret))
                //注册事件监听
                .registerAllEventListener(event -> {
                    try {
                        // 发布系统开发的事件
                        publisher.performAction(event);
                        // 消费成功
                        return EventAckStatus.SUCCESS;
                    } catch (Exception e) {
                        //消费失败
                        return EventAckStatus.LATER;
                    }
                })
                .build()
                .start();
    }

}
